package logger

import (
	"core/config"
	gostlog "github.com/dubbogo/gost/log/logger"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"

	"os"
)

var (
	Config = &LogConfig{}

	Logger        *zap.Logger
	SugaredLogger *zap.SugaredLogger
)

func init() {
	config.ConfigViper.UnmarshalKey("logger", Config)

	logMode := zapcore.DebugLevel
	switch Config.Level {

	case "info":
		logMode = zapcore.InfoLevel
	case "warn":
		logMode = zapcore.WarnLevel
	case "error":
		logMode = zapcore.ErrorLevel
	default:
		logMode = zapcore.DebugLevel
	}

	//fmt.Println("mode", logMode)
	//
	//// 第一个参数是输出的格式 第二个参数 输出的位置
	//
	//zapcore.NewMultiWriteSyncer 输出到多个终端 比如 文件 console中
	core := zapcore.NewCore(getEncoder(), zapcore.NewMultiWriteSyncer(getWriterSyncer(), zapcore.AddSync(os.Stdout)), logMode)

	Logger = zap.New(core, zap.AddCaller())
	SugaredLogger = Logger.Sugar()
	gostlog.SetLogger(SugaredLogger)

}

func Sync() {
	Logger.Sync()
}
